<template>
  <el-dialog
    title="新增员工"
    width="600px"
    :visible.sync="show"
    @close="closeEvent"
  >
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment">
          <el-option
            v-for="item in employeesData.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="form.departmentName" readonly @focus="focusEvent" />
        <el-collapse-transition>
          <el-tree
            v-if="showTree"
            :data="treeList"
            :props="{ label: 'name' }"
            @node-click="nodeClick"
          />
        </el-collapse-transition>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="form.timeOfEntry" value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="form.correctionTime"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import employeesData from '@/api/constant/employees'
import { sysUserPost } from '@/api/employees'
import { companyDepartment } from '@/api/departments'
export default {
  data() {
    return {
      show: false,
      employeesData,
      treeList: [],
      showTree: false,
      xxx: '',
      form: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }],
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            pattern: /^1[2-9]\d{9}$/,
            message: '请输入正确的手机号',
            trigger: 'change'
          }
        ],
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '必填', trigger: 'change' }
          // {
          //   validator: (rule, value, callback) => {
          //     const bol = this.initList.every((item) => {
          //       return item.code !== value
          //     })
          //     if (bol) {
          //       callback()
          //     } else {
          //       callback(new Error('重复的工号:' + value))
          //     }
          //   },
          //   trigger: 'change'
          // }
        ],
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }],
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await sysUserPost(this.form)
          this.$message.success('新增员工成功')
          this.show = false
          this.$emit('getData')
        }
      })
    },
    cancel() {
      this.show = false
      this.$refs.form.resetFields()
      this.form = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
    },
    closeEvent() {
      this.$refs.form.resetFields()
      this.form = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
    },
    // 获取组织架构数据
    async focusEvent() {
      this.showTree = true
      if (this.treeList.length === 0) {
        const res = await companyDepartment()
        this.treeList = this.changeData(res.data.depts, '')
      }
    },
    // 树形数据转换
    changeData(arr, pid) {
      return arr.filter((item) => {
        if (item.pid === pid) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // tree点击事件
    nodeClick(row) {
      this.form.departmentName = row.name
      this.showTree = false
    }
  }
}
</script>

<style></style>
